package com.rsstuan.storage.impl;

import java.util.List;

import javax.jdo.PersistenceManager;

import com.rsstuan.storage.data.TuanGouSiteData;

public class JDOTuanGouSiteStorage {
	
	public boolean saveSite(TuanGouSiteData site) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(site);
			return true;
		} 
		catch (Exception e) {
			return false;
		}
		finally {
			pm.close();
		}
	}
	
	public TuanGouSiteData getSite(Long id) {
		 PersistenceManager pm = PMF.get().getPersistenceManager(); 
	        String query = "select from " + TuanGouSiteData.class.getName()
	                + " where id ==" + id;
	        TuanGouSiteData site = (TuanGouSiteData) pm.newQuery(query).execute();
	        return site;
	}
	
	public TuanGouSiteData getSite(String siteName) {
		 PersistenceManager pm = PMF.get().getPersistenceManager(); 
	        String query = "select from " + TuanGouSiteData.class.getName()
	                + " where siteName =='" + siteName +"'";
	        TuanGouSiteData site = (TuanGouSiteData) pm.newQuery(query).execute();
	        return site;
	}

    /**
     * @return
     */
    public List<TuanGouSiteData> getAllSites() {
        PersistenceManager pm = PMF.get().getPersistenceManager(); 
        String query = "select from " + TuanGouSiteData.class.getName();
        List<TuanGouSiteData> sites = (List<TuanGouSiteData>) pm.newQuery(query).execute();
        return sites;
}

}
